package com.fx.eduservice.controller;


import com.fx.commonuntils.ErrorEnum;
import com.fx.commonuntils.ReturnMessage;
import com.fx.commonuntils.ReturnMessageUtils;
import com.fx.commonuntils.guavauntils.CheckParamUtils;
import com.fx.eduservice.entity.vo.EduChapter;
import com.fx.eduservice.entity.vo.EduVideo;
import com.fx.eduservice.request.CourseInfoVo;
import com.fx.eduservice.response.curstom.CustrmResponse;
import com.fx.eduservice.service.*;
import com.google.common.base.Preconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Objects;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author testjava
 * @since 2020-11-07
 */
@Api(description = "课程管理系统")
@RestController
@RequestMapping("/eduservice/educourse")
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
@Slf4j
@CrossOrigin
public class EduCourseController {
    final private EduCourseService eduCourseService;
    final private EduVideoService eduVideoService;
    final private EduChapterService eduChapterService;
    final private EduCourseDescriptionService eduCourseDescriptionService;
    final private EduSubjectService eduSubjectService;


    @ApiOperation(value = "添加课程信息")
    @PostMapping("/addCourseInfo")
    public ReturnMessage<Object> addCourseInfo(@RequestBody @Valid CourseInfoVo courseInfoVo, BindingResult br) {
        log.info("Request:{}", courseInfoVo);
        if (courseInfoVo == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return eduCourseService.addCourseInfo(courseInfoVo);


    }


    @ApiOperation(value = "查询回写课程信息")
    @GetMapping("/getCourseInfoById")
    public ReturnMessage<Object> getCourseInfoById(@ApiParam(name = "id", value = "课程id", required = true)
                                                   @RequestParam String id) {

        CheckParamUtils.isNotNullAndEmty(id, "课程id");

        return eduCourseService.getCourseInfoById(id);

    }

    @ApiOperation(value = "修改课程信息")
    @PostMapping("/updataCourseInfo")
    public ReturnMessage<Object> updataCourseInfo(@RequestBody @Valid CourseInfoVo courseInfoVo, BindingResult br) {
        log.info("Request:{}", courseInfoVo);
        if (courseInfoVo == null) {
            return ReturnMessageUtils.errorWithEum(ErrorEnum.E_REQUEST_PARAM_ERRORJSON);
        }
        if (br.hasErrors()) {
            return ReturnMessageUtils.error(ErrorEnum.E_REQUEST_PARAM_ERROR.getErrorCode(), Objects.requireNonNull(br.getFieldError()).getDefaultMessage());
        }
        return eduCourseService.updataCourseInfo(courseInfoVo);


    }

    @ApiOperation(value = "根据课程信息反馈课程确认信息表")
    @GetMapping("/quaryCustormPublic")
    public ReturnMessage<CustrmResponse> quaryCustormPublic(@ApiParam(name = "id", value = "课程id", required = true)
                                                    @RequestParam String id) {

        CheckParamUtils.isNotNullAndEmty(id, "课程id");

        return eduCourseService.quaryCustormPublic(id);

    }

    @ApiOperation(value = "根据课程id逐步删除课程测试")
    @GetMapping("/deleteCustormPublic")
    public ReturnMessage<CustrmResponse> deleteCustormPublic(@ApiParam(name = "id", value = "课程id", required = true)
                                                            @RequestParam String id) {

        CheckParamUtils.isNotNullAndEmty(id, "课程id");
        //小节删除
        eduVideoService.deleteVideoByCoursmId(id);
        //删除章
        eduChapterService.deleteChapterByCoursmId(id);
        //删除简介
        eduCourseDescriptionService.removeById(id);
        //删除本身
        eduCourseService.removeById(id);

        return ReturnMessageUtils.success();

    }


    //根据课程id查询出课程信息，需要展示课程id，课程封面，课程的名称，讲师的名称



















}






